package jpb.module.service.load;

import jpb.constant.UpState;
import jpb.module.daomain.member.Member;
import jpb.module.daomain.member.LoadMem;
import jpb.module.daomain.organize.Organize;
import jpb.module.daomain.profession.Profession;
import jpb.module.service.department.DepartmentService;
import jpb.module.service.member.MemberService;
import jpb.module.service.organize.OrganizeService;
import jpb.module.service.profession.ProfessionService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by haol on 2017/4/18.
 */
@Service
public class ProofMessageService {
        private Logger logger = LoggerFactory.getLogger(ProofMessageService.class);
        @Autowired
        private DepartmentService departmentService;

        @Autowired
        private ProfessionService professionService;

        @Autowired
        private MemberService memberService;
        @Autowired
        private OrganizeService organizeService;


        public List<LoadMem> add(List<LoadMem> loadMems) {
                for (LoadMem member : loadMems) {
                        if (member.getErrorMessage() == null) {
                                boolean update = organizeService.checkIsExist(member.getOid(),
                                        member.getMemberNum());
                                if (update) {
                                        member.setUpState(UpState.UPDATE);
                                        organizeService.updateOrgMem(member.getMemberNum(), member.getOid(),
                                                member.getPid(),member.getDid());
                                        logger.info("成员编号为[{}]的信息已经存在，此时更新成功",
                                                member.getMemberNum());
                                } else {
                                        member.setUpState(UpState.ADD);
                                        organizeService.addMem(
                                                member.getOid(), member.getDid(), member.getMemberNum(),member.getDid());
                                }
                                memberService.updatePro(
                                        member.getMemberNum(), member.getOid(), member.getPid());
                        } else {
                                member.setUpState(UpState.ERROR);
                                logger.info("编号为为[{}]的存在错误信息[{}]未上传成功",
                                        member.getMemberNum(), member.getErrorMessage());
                        }
                }
                return loadMems;
        }

        public List<LoadMem> proofMessage(List<LoadMem> loadMems) {
                List<LoadMem> members = new ArrayList<LoadMem>();
                for (LoadMem member : loadMems) {
                        if (member.getProName().isEmpty()) {
                                member.setErrorMessage("职称不能为空");
                        } else {
                                Profession profession = professionService.getByOidAndName(
                                        member.getOid(), member.getProName());
                                if (profession == null) {
                                        member.setErrorMessage("职称未找到");
                                } else {
                                        member.setPid(profession.getId());
                                }
                        }
                        members.add(member);
                }

                return members;
        }

        public List<LoadMem> addLeaguer(List<LoadMem> members) {
                for (LoadMem leaguer : members) {
                        if (leaguer.getErrorMessage() == null) {
                                Member member = memberService.get(leaguer.getMemberNum());
                                if (member == null) {
                                        memberService.complete(leaguer);
                                        organizeService.addMem(
                                                leaguer.getOid(),leaguer.getDid(),leaguer.getMemberNum(),leaguer.getPid());
                                } else {
                                        memberService.update(leaguer);
                                }
                        } else {
                                leaguer.setUpState(UpState.ERROR);
                                logger.info("编号为为[{}]的存在错误信息[{}]未上传成功",
                                        leaguer.getMemberNum(), leaguer.getErrorMessage());
                        }
                }
                return members;
        }

        public List<LoadMem> proofMessageLeaguer(List<LoadMem> loadMems) {
            Organize organize = organizeService.get(loadMems.get(0).getOid());
                for (LoadMem member : loadMems) {
                        if (StringUtils.isBlank(member.getMobile())) {
                                member.setErrorMessage(member.getErrorMessage() + "电话不能为空,");
                        }else {
                                List<String> mobiles = new ArrayList<String>();
                                String[] mobile = member.getMobile().split("/");
                                for (String mob:mobile){
                                        if (mob.length()<=11){
                                                mobiles.add(mob);
                                        }else {
                                                member.setErrorMessage(member.getErrorMessage() + "电话号码过长,");
                                        }
                                }
                                member.setMobiles(mobiles);
                        }

                        member.setName(member.getName().replaceAll(" ", ""));
                        if (StringUtils.isBlank(member.getName())) {
                                member.setErrorMessage(member.getErrorMessage() + "姓名不能为空,");
                        } else if (member.getName().length() > 6) {
                                member.setErrorMessage(member.getErrorMessage() + "姓名过长,");
                        }
                        if (StringUtils.isBlank(member.getMemberNum())) {
                                member.setErrorMessage(member.getErrorMessage() + "编号不能为空");
                        } else {
                                member.setMemberNum(organize.getMark() +"@"+ member.getMemberNum());
                                int size = member.getMemberNum().length();
                                String st = "";
                                while (size < 20){
                                        st+="#";
                                        size++;
                                }
                                member.setMemberNum(member.getMemberNum()+st);
                        }
                }

                return loadMems;
        }
}
